<!--
 * @Author: xuxu12138 1718324422@qq.com
 * @Date: 2023-09-04 08:25:22
 * @LastEditors: xuxu12138 1718324422@qq.com
 * @LastEditTime: 2023-10-10 10:33:06
 * @FilePath: /things-admin/src/view/dataScreen/components/time.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div >{{ time }}</div>
</template>

<script>
export default {
  data() {
    return {
      time: "",
      timer: null,
    };
  },
  methods: {
    //获取当前日期
    getTmie() {
      // 获取当前时间
      var now = new Date();
      // 将时间转化为指定格式的字符串
      var year = now.getFullYear();
      var month = now.getMonth() + 1;
      var day = now.getDate();
      var week = ["日", "一", "二", "三", "四", "五", "六"][now.getDay()];
      var hour = now.getHours();
      // 时分秒个位补零
      if (hour >= 0 && hour < 10) {
        hour = "0" + hour;
      }
      var minute = now.getMinutes();
      if (minute >= 0 && minute < 10) {
        minute = "0" + minute;
      }
      var second = now.getSeconds();
      if (second < 10) {
        second = "0" + second;
      }
      var timeStr =
        year +
        "年" +
        month +
        "月" +
        day +
        "日 " +
        "星期" +
        week +
        " " +
        hour +
        ":" +
        minute +
        ":" +
        second;
      this.time = timeStr;
    },
    startTimer() {
      // 每一秒刷新一次时间
      this.timer = setInterval(() => {
        this.getTmie();
      }, 1000);
    },
    stopTimer() {
      clearInterval(this.timer);
    },
  },

  created() {
    this.startTimer();
  },
  //销毁定时器等一些全局事件
  beforeDestroy() {
    this.stopTimer();
  },
};
</script>

<style lang="less" scoped>

</style>
